package controllers;

import business.SysUser;
import models.sys_user;
import play.cache.Cache;
import play.mvc.Controller;
import play.mvc.Scope;
import business.Constants;
import utils.ErrorInfo;

/**
 * 后台登录
 */
public class BackendLogin extends Controller {
    /**
     * 后台登录页面
     */
    public static void index() {
        render();
    }
    /**
     * 后台登录主页面
     * /BackendLogin/backendmain
     */
    public static void backendmain(){
        SysUser.checkLogin();
        sys_user user = SysUser.getSessionUser();
        render(user);
    }
    public static void welcome(){
        render();
    }
    public static void logout(){
        //退出逻辑
        //...
        //跳转后台登录
        String sessionId = Scope.Session.current().getId();
        Cache.delete(Constants.USERFIX+sessionId);
        index();
    }
    public static void login(String username,String password){
        ErrorInfo errorInfo = new ErrorInfo();
        sys_user sysUser =  SysUser.login(username,password);
        if(sysUser!=null){
            SysUser.setSessionUser(sysUser);
            errorInfo.code=1;
            errorInfo.msg="登录成功";
        }else{
            errorInfo.code = 0;
            errorInfo.msg = "登录失败,请重新登录";
        }
        renderJSON(errorInfo);
    }
}
